import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.awt.*;
import javax.swing.tree.*;
import javax.swing.border.*;
import java.rmi.*;


public class MCQTest extends JInternalFrame implements ClientInterface,passwordInterface
{
	public JPanel pan = new JPanel();
	public String ra1[] = new String[500];
	public static int s = 1;
	public static int s0 = 0;
	public static int s1 = 1;
	public static int s2 = 2;
	public static int s3 = 3;
	public static int s4 = 4;
	public static int s5 = 5;
	public static int result = 0;
	public static int resul = 0;
	public MCQTest()
	{
		super("MCQ Test",false,true,false,false);
		this.setBounds(30,10,700,480);
		setVisible(true);
		
		Container c = getContentPane();
		c.setLayout(null);
		
		lab12.setBounds(360,20,125,25);
		lab12.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab12);
		lab13.setBounds(470,20,180,25);
		lab13.setFont(new Font("Verdana",Font.BOLD,14));
		c.add(lab13);
		lab13.setText(usersName.getText());
		
		lab14.setBounds(50,20,130,25);
		lab14.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab14);
		lab15.setBounds(182,20,35,25);
		lab15.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab15);
		lab16.setBounds(50,45,160,25);
		lab16.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab16);
		lab17.setBounds(212,45,70,25);
		lab17.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab17);
		
		lab18.setBounds(80,75,160,25);
		lab18.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab18);
		lab19.setBounds(190,75,30,25);
		lab19.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(lab19);
		jsp5.setBounds(80,105,460,70);
		c.add(jsp5);
		
		abutt.setBounds(80,185,40,20);
		abutt.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(abutt);
		afield.setBounds(120,185,390,22);
		c.add(afield);
		bbutt.setBounds(80,210,40,20);
		bbutt.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(bbutt);
		bfield.setBounds(120,210,390,22);
		c.add(bfield);
		cbutt.setBounds(80,235,40,20);
		cbutt.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(cbutt);
		cfield.setBounds(120,235,390,22);
		c.add(cfield);
		dbutt.setBounds(80,260,40,20);
		dbutt.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(dbutt);
		dfield.setBounds(120,260,390,22);
		c.add(dfield);
		
		btgroup.add(abutt);
		btgroup.add(bbutt);
		btgroup.add(cbutt);
		btgroup.add(dbutt);
		
		bt4.setBounds(407,285,110,35);
		bt4.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(bt4);
		bt5.setBounds(287,285,110,35);
		bt5.setFont(new Font("Verdana",Font.PLAIN,14));
		c.add(bt5);
		bt5.setEnabled(false);
		pan.setLayout(null);
		pan.setBounds(80,325,450,100);
		pan.setBackground(Color.gray);
		pan.setBorder(new LineBorder(Color.red,2));
		c.add(pan);
		
		lab20.setBounds(40,5,180,35);
		lab20.setFont(new Font("Verdana",Font.PLAIN,14));
		pan.add(lab20);
		lab21.setBounds(220,5,50,35);
		lab21.setFont(new Font("Verdana",Font.PLAIN,14));
		pan.add(lab21);
		pan.setVisible(false);
		lab22.setBounds(40,40,210,35);
		lab22.setFont(new Font("Verdana",Font.PLAIN,14));
		pan.add(lab22);
		lab23.setBounds(250,40,50,35);
		lab23.setFont(new Font("Verdana",Font.PLAIN,14));
		pan.add(lab23);
		
		RadioButtonHandler handler = new RadioButtonHandler();
		abutt.addItemListener(handler);
		bbutt.addItemListener(handler);
		cbutt.addItemListener(handler);
		dbutt.addItemListener(handler);
		
		try {
			String ur = "rmi://"+clientconf.getText()+"/MainServer";
			MainServerInterface msin = (MainServerInterface)Naming.lookup(ur);
			for(int i=0;i<500;i++)
			{
			ra1[i] = "";
			}
			ra1 = msin.McqQuestion("");
			String ra2 = msin.McqTime("");
			String ra3 = msin.ToQues("");
			lab15.setText(ra3);
			lab17.setText(ra2);
			
			lab19.setText(Integer.toString(s));
			txa5.setText(ra1[s0]);
			afield.setText(ra1[s1]);
			bfield.setText(ra1[s2]);
			cfield.setText(ra1[s3]);
			dfield.setText(ra1[s4]);
			
			
			Theread thr1 = new Theread("Thread1");
			thr1.start();
			
		}
		catch(Exception x) {
			JOptionPane.showMessageDialog(null,"IIOP Error..\n please Contact to the Administrator","Error",JOptionPane.ERROR_MESSAGE);
		}
		
		bt4.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			if((ra1[s0+6]).equals(""))
			{
				//try {
				//String ur = "rmi://127.0.0.1/MainServer";
				//MainServerInterface msin = (MainServerInterface)Naming.lookup(ur);
				//String ra9 = msin.McqResult(Integer.toString(result),lab13.getText());
					
				//}
				//catch(Exception t){
				//	JOptionPane.showMessageDialog(null,"Internal Server Error..\n Contact to the Administrator","Error",JOptionPane.ERROR_MESSAGE);
				//}
				
				JOptionPane.showMessageDialog(null,"You Have answered all the question..","Information",JOptionPane.INFORMATION_MESSAGE);
				bt4.setEnabled(false);
				bt5.setEnabled(true);
				txa5.setText("");
				afield.setText("");
				bfield.setText("");
				cfield.setText("");
				dfield.setText("");
			}
			else {
				s = s + 1;
				lab19.setText(Integer.toString(s));
				s0 = s0 + 6;
				txa5.setText(ra1[s0]);
				s1 = s1 + 6;
				afield.setText(ra1[s1]);
				s2 = s2 + 6;
				bfield.setText(ra1[s2]);
				s3 = s3 + 6;
				cfield.setText(ra1[s3]);
				s4 = s4 + 6;
				dfield.setText(ra1[s4]);
				abutt.setEnabled(true);
				bbutt.setEnabled(true);
				cbutt.setEnabled(true);
				dbutt.setEnabled(true);
				
				
				//abutt.setSelected(false);
				//bbutt.setSelected(false);
				//cbutt.setSelected(false);
				//dbutt.setSelected(false);
			}
			result = result + resul;
				s5 = s5 + 6;
		}
		}
		);
		
		bt5.addActionListener(new ActionListener()
		{
		public void actionPerformed(ActionEvent e)
		{
			pan.setVisible(true);
			lab21.setText(lab15.getText());
			lab23.setText(Integer.toString(result));
			try {
			String ur = "rmi://"+clientconf.getText()+"/MainServer";
				MainServerInterface msin = (MainServerInterface)Naming.lookup(ur);
				String ra9 = msin.McqResult(Integer.toString(result),lab13.getText());
			}
			catch(Exception ex){
				JOptionPane.showMessageDialog(null,"Internal Server Error..\n Contact to the Administrator","Error",JOptionPane.ERROR_MESSAGE);
			}
			
		}
		}
		);
		try
		{
			UIManager.setLookAndFeel(looks[1].getClassName());
			SwingUtilities.updateComponentTreeUI(this);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	private class RadioButtonHandler implements ItemListener {
		public void itemStateChanged(ItemEvent ev) {
			if(ev.getSource() == abutt && (ra1[s5].equals("a") || ra1[s5].equals("A")))
			{
				resul = 1;
				abutt.setEnabled(false);
			bbutt.setEnabled(false);
			cbutt.setEnabled(false);
			dbutt.setEnabled(false);
				
			}
			
			else if(ev.getSource() == bbutt && (ra1[s5].equals("b") || ra1[s5].equals("B")))
			{
				resul = 1;
				abutt.setEnabled(false);
			bbutt.setEnabled(false);
			cbutt.setEnabled(false);
			dbutt.setEnabled(false);
				
			}
			
			else if(ev.getSource() == cbutt && (ra1[s5].equals("c") || ra1[s5].equals("C")))
			{
				resul = 1;
				abutt.setEnabled(false);
			bbutt.setEnabled(false);
			cbutt.setEnabled(false);
			dbutt.setEnabled(false);
				
			}
			
			else if(ev.getSource() == dbutt && (ra1[s5].equals("d") || ra1[s5].equals("D")))
			{
				resul = 1;
				abutt.setEnabled(false);
			bbutt.setEnabled(false);
			cbutt.setEnabled(false);
			dbutt.setEnabled(false);
				
			}
			else {
			abutt.setEnabled(false);
			bbutt.setEnabled(false);
			cbutt.setEnabled(false);
			dbutt.setEnabled(false);
			resul = 0;
			}
		}
		
	}
	
}
